package com.dtz.rabbitmq.workqueuen.lunxun;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
	
	public static void main(String[] args) {
		// 1、创建连接工厂
		ConnectionFactory factory = new ConnectionFactory();
		factory.setVirtualHost("/");
		factory.setHost("1.15.137.13");
		factory.setPort(5672);
		factory.setUsername("admin");
		factory.setPassword("admin");
		
		Connection connection = null;
		Channel channel = null;
		
		try {
			
			connection = factory.newConnection();
			
			channel = connection.createChannel();
			
			// 声名队列
			String queuen1 = "WR-queuen1";
			channel.queueDeclare(queuen1, true, false, false, null);
			
			// 发送消息
			for (int i = 0; i < 20; i++) {
				String msg = "test" + i;
				channel.basicPublish("", queuen1, null, msg.getBytes());
			}
			
			System.out.println("发送消息成功");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally
		{
			// 9: 释放连接关闭通道
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
		}
	}
	
}
